$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'SchoolAnnouncement/Select',
        datatype: "json",
        colModel: [
            { label: '公告id', name: 'schoolAnnouncementId',key: true,hidden:true },
             { label: '标题', name: 'schoolAnnouncementTitle',width:200},
            { label: '内容', name: 'schoolAnnouncementContent',
                hidden:true
            },
             { label: '更新时间', name: 'schoolAnnouncementUpdateTime'},
             { label: '创建人', name: 'schoolAnnouncementCreater'},
             { label: '类型id', name: 'schoolAnnouncementTypeId' ,hidden:true},
             { label: '创建时间', name: 'schoolAnnouncementCreateTime'},
             { label: '类型', name: 'schoolAnnouncementType', hidden:true },
             { label: '类型名称', name: 'schoolAnnouncementTypeName',hidden:true },
             { label: '浏览次数', name: 'schoolAnnouncementViews',hidden:true },
            { label: '是否置顶', name: 'schoolAnnouncementSticky', formatter: function(value, options, row){
                return value == 0 ?
                    '<span class="table-btn1 isNot">否</span>' :
                    '<span class="table-btn1 isSure">是</span>';
            }},
             { label: '置顶时长', name: 'schoolAnnouncementStickTime',hidden:true }
         ],
        viewrecords: true,
        autowidth:true,
        height: '100%',
        rowNum: 10,
        rowList : [10,30,50],
        // rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        SchoolAnnouncement: {
            schoolAnnouncementSticky:"0"
        },
        schoolAnnouncementId: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.SchoolAnnouncement = {
                schoolAnnouncementSticky:"0"
            };
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "SchoolAnnouncement/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.SchoolAnnouncement = r.SchoolAnnouncement;
                vm.schoolAnnouncementId = r.SchoolAnnouncement.schoolAnnouncementId;
            });
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "SchoolAnnouncement/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            var url = vm.SchoolAnnouncement.schoolAnnouncementId !=null ? "SchoolAnnouncement/Update" : "SchoolAnnouncement/Insert";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.SchoolAnnouncement),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'schoolAnnouncementTitle': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        },
        validator: function () {
            if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementTitle)){
                alert("标题不能为空");
                return true;
            }
            if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementContent)){
                alert("内容不能为空");
                return true;
            }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementUpdateTime)){
            //     alert("更新时间不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementCreater)){
            //     alert("创建人不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementTypeId)){
            //     alert("类型id不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementCreateTime)){
            //     alert("创建时间不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementType)){
            //     alert("类型不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementTypeName)){
            //     alert("类型名称不能为空");
            //     return true;
            // }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementViews)){
            //     alert("浏览次数不能为空");
            //     return true;
            // }
            if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementSticky)){
                alert("是否置顶不能为空");
                return true;
            }
            // if(isBlank(vm.SchoolAnnouncement.schoolAnnouncementStickTime)){
            //     alert("置顶时长不能为空");
            //     return true;
            // }
            }
        }
});

